/**
 * 
 */
package br.ufal.ic.sd.project.server.services;

import java.io.IOException;

import br.ufal.ic.sd.project.server.core.Channel;
import br.ufal.ic.sd.project.server.core.Replicator;
import br.ufal.ic.sd.project.server.core.Server;
import br.ufal.ic.sd.project.server.core.ServerService;
import br.ufal.ic.sd.project.server.exception.ServerException;
import br.ufal.ic.sd.project.server.helper.Helper;

/**
 * @author keven andrade
 *
 */
public class LoggerService extends ServerService{

    @Override
    public void executeService(Server objServer) {

	String strProjectName = "";
	String strLogType = "";
	String strTimeLogOcurrence = "";
	String strUser = "";
	String strClass = "";
	String strLogMessage = "";
    	
	
	try {
		
        	strProjectName = objServer.getObjChannel().read();
        	
        	System.out.println(strProjectName);
        	
        	if(Helper.isProject(Helper.getServerRepository(), strProjectName)){
        	    
        	    objServer.getObjChannel().write("OK");
        	    
        	}else{
        	    
        	    if(Helper.createNewProject(Helper.getServerRepository(), strProjectName)){
        		objServer.getObjChannel().write("OK");
        	    }else{
        		objServer.getObjChannel().write("NOK");
        		objServer.getObjChannel().getObjOutputStreamOut().flush();
        		objServer.getObjChannel().write("Error creating projet log");
        		return;
        	    }
        	}
        	
        	strLogType = objServer.getObjChannel().read();
        	strTimeLogOcurrence = objServer.getObjChannel().read();
        	strUser = objServer.getObjChannel().read();
        	strClass = objServer.getObjChannel().read();
        	strLogMessage = objServer.getObjChannel().read();
        	
        	System.out.println(strLogType);
        	System.out.println(strTimeLogOcurrence);
        	System.out.println(strUser);
        	System.out.println(strClass);
        	System.out.println(strLogMessage);
        	
        	objServer.getObjChannel().write("OK");
     	
        	objServer.getObjChannel().getObjClientSocket().close();
        	
        	if(objServer.getObjServerTokken().hasPermisson()){
         	  
        	    String strFileName = Helper.addLog(objServer.getStrRepository(), strProjectName, strTimeLogOcurrence, strUser, strClass, strLogType, strLogMessage);
        	    doReplication(strFileName);
         	   
         	}else{
         	    
         	    while(!objServer.getObjServerTokken().hasPermisson()){
         			
         		try {
			    this.wait(2000);
			} catch (InterruptedException e) {
			    e.printStackTrace();
			}
         		
         	    }
         	    
         	    if(objServer.getObjServerTokken().hasPermisson()){
         		
         		String strFileName = Helper.addLog(objServer.getStrRepository(), strProjectName, strTimeLogOcurrence, strUser, strClass, strLogType, strLogMessage);
         	    	doReplication(strFileName);
         	    }
         	    
         	}
    	
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }
    
    private void doReplication(String strFileName) {
	
	Replicator objReplicator = new Replicator(Helper.getServerRepository(), strFileName);
	objReplicator.reply();
    }

}
